package com.juyu.reader.pc.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.juyu.reader.pc.common.Constants;
import com.juyu.reader.pc.common.PageDirective;
import com.juyu.reader.pc.model.Book;
import com.juyu.reader.pc.model.BookChapter;
import com.juyu.reader.pc.model.Category;
import com.juyu.reader.pc.model.Page;
import com.juyu.reader.pc.service.BookChapterService;
import com.juyu.reader.pc.service.BookService;
import com.juyu.reader.pc.service.CategoryService;

@Controller
@RequestMapping("/stack")
public class StackController extends BaseController {

	@Autowired
	private BookService bookService;
	@Autowired
	private BookChapterService bookChapterService;
	@Autowired
	private CategoryService categoryService;

	/**
	 * 书库
	 * @param request
	 * @param response
	 * @param searchBook
	 * @param page
	 * @return
	 */
	@RequestMapping("/index")
	public ModelAndView stackIndex(HttpServletRequest request, HttpServletResponse response, Book searchBook,
			Page page) {
		this.setNavIndex(request, 2);
		page.setLimit(20);
		searchBook.setLimit(page.getLimit());
		searchBook.setPageNo(page.getPageNo());
		Map<String, Object> attrMap = new HashMap<String, Object>();
		List<Book> bookList = bookService.findBookByStack(searchBook);
		int count = bookService.countBookByStack(searchBook);
		for (Book b : bookList) {
			List<BookChapter> latestChapters = bookChapterService.findBookChapterLatest(b.getBookId());
			if (latestChapters.size() >= 1) {
				b.setBookLatestChapterId(latestChapters.get(0).getChapterId());
				b.setBookLatestChapter(latestChapters.get(0).getChapterName());
			}
		}
		/**
		 * 小说分类类型列表  t_category表
		 */
		List<Category> categoryList = categoryService.findChildCategory();
		attrMap.put("sourceUrl", Constants.SOURCE_URL);
		attrMap.put("bookList", bookList);
		attrMap.put("recordCount", count);
		attrMap.put("searchBook", searchBook);
		attrMap.put("page", page);
		attrMap.put("pageftl", new PageDirective(request, page.getLimit()));
		attrMap.put("categoryList", categoryList);

		/**
		 * 右侧点击榜 t_book
		 */
		Book findClickBook = new Book();
		findClickBook.setBookOrderBy(1);// 默认人气排序，1人气排序，2字数排序，3更新时间排序，4打赏排行，5推荐排行
		findClickBook.setLimit(10);
		List<Book> bookClickList = bookService.findBookOrderBy(findClickBook);
		// 排行榜-点击
		if (bookClickList.size() >= 10) {
			attrMap.put("bookClick1", bookClickList.get(0));
			attrMap.put("bookClickList", bookClickList.subList(1, 10));
		}
		/**
		 * 右侧强推榜 t_book
		 */
		findClickBook.setBookOrderBy(5);// 默认人气排序，1人气排序，2字数排序，3更新时间排序，4打赏排行，5推荐排行
		findClickBook.setLimit(10);
		List<Book> bookForwardList = bookService.findBookOrderBy(findClickBook);
		// 推荐榜-强推榜
		if (bookForwardList.size() >= 10) {
			attrMap.put("bookForward1", bookForwardList.get(0));
			attrMap.put("bookForwardList", bookForwardList.subList(1, 10));
		}

		return this.packModeAndView(request, "/book/stackList", null, attrMap);
	}
}
